//
//  Account.m
//  
//
//  Created by barak on 10/27/12.
//  Copyright (c) 2012 yaniv. All rights reserved.
//

#import "Account.h"

@implementation Account

@synthesize accountNumber;
@synthesize actualBalance;
@synthesize availableBalance;
@synthesize currency;
@synthesize holders;
@synthesize iban;
@synthesize accountId;
@synthesize office;
@synthesize retainedBalance;

static Account* mainAccount;
+ (Account *)instanceFromDictionary:(NSDictionary *)aDictionary {

    Account *instance = [[Account alloc] init];
    [instance setAttributesFromDictionary:aDictionary];
    mainAccount = instance;
    return instance;

}

+(Account*) mainAccount {
    return mainAccount;
}

- (void)setAttributesFromDictionary:(NSDictionary *)aDictionary {

    if (![aDictionary isKindOfClass:[NSDictionary class]]) {
        return;
    }

    [self setValuesForKeysWithDictionary:aDictionary];

}

- (void)setValue:(id)value forKey:(NSString *)key {

    if ([key isEqualToString:@"holders"]) {

        if ([value isKindOfClass:[NSArray class]]) {

            NSMutableArray *myMembers = [NSMutableArray arrayWithCapacity:[value count]];
            for (id valueMember in value) {
                [myMembers addObject:valueMember];
            }

            self.holders = myMembers;

        }

    } else {
        [super setValue:value forKey:key];
    }

}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

    if ([key isEqualToString:@"id"]) {
        [self setValue:value forKey:@"accountId"];
    } else {
        [super setValue:value forUndefinedKey:key];
    }

}


- (NSDictionary *)dictionaryRepresentation {

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    if (self.accountNumber) {
        [dictionary setObject:self.accountNumber forKey:@"accountNumber"];
    }

    if (self.actualBalance) {
        [dictionary setObject:self.actualBalance forKey:@"actualBalance"];
    }

    if (self.availableBalance) {
        [dictionary setObject:self.availableBalance forKey:@"availableBalance"];
    }

    if (self.currency) {
        [dictionary setObject:self.currency forKey:@"currency"];
    }

    if (self.holders) {
        [dictionary setObject:self.holders forKey:@"holders"];
    }

    if (self.iban) {
        [dictionary setObject:self.iban forKey:@"iban"];
    }

    if (self.accountId) {
        [dictionary setObject:self.accountId forKey:@"accountId"];
    }

    if (self.office) {
        [dictionary setObject:self.office forKey:@"office"];
    }

    if (self.retainedBalance) {
        [dictionary setObject:self.retainedBalance forKey:@"retainedBalance"];
    }

    return dictionary;

}

@end
